const api = require('../../config/api.js');//url
const util = require('../../utils/util.js');//请求方法
const app = getApp();
Page({
  data: {
    info: {
      meetingDocumentsUrl:'',
      meetingDocumentsName:'',
      title: '',
      timeStart: '',
      image: '',
      detail: '',
      location:'',
      timeEnd:''
    },
    id: ''
  },
  onShareAppMessage () {
    return {};
  },
  onLoad (options) {
    const that = this;
    that.setData({
      id:options.id
    })
    util.request(api.ActivityDetail, { id: options.id }, 'get').then(function (res) {
      if (res.errno === 200) {
        const data = res.data;
        // console.log(data)
        const info = {
          meetingDocumentsUrl:data.meetingDocumentsUrl,
          meetingDocumentsName:data.meetingDocumentsName,
          title: data.title,
          timeStart: data.timeStart && util.formatTime(new Date(data.timeStart)),
          image: data.pictureUrl,
          detail: data.detail,
          baseUrl: data.fdfsBaseUrl,
          location:data.location,
          timeEnd: data.timeEnd && util.formatTime(new Date(data.timeEnd)),
        }
        that.setData({
          info,
        });
      }
    });
  },
  view_5OnClick(e) {
    const that = this;

    const now = new Date();
    const timeEnd = new Date(this.data.info.timeEnd); // 确保timeEnd是正确格式化的日期对象
    console.log("活动结束时间"+timeEnd)
    if (now > timeEnd) {
      wx.showToast({
        title: '活动已结束！',
        icon: 'none',
        duration: 2000
      });
      return
    }

    /*
        console.log("that.id"+parseInt(that.data.id))
        console.log("that.userId"+wx.getStorageSync('userInfo').id)
    */
    util.request(api.ActivityApplyPage, {
      activityId: parseInt(that.data.id),
      userId: wx.getStorageSync('userInfo').id,
      pageRequestVo: {
        pageNum: this.data.page,
        pageSize: 10,
      }
    }, 'POST').then(function (res) {

      const isApply = res.data.total;

      const hasLogin = app.globalData.hasLogin;
      if (!hasLogin) {
        wx.navigateTo({ url: "/pages/Login/Login" });
      } else if (isApply === 1) {
        wx.showToast({
          title: '您已报过名！',
          icon: 'none',
          duration: 2000
        });
      } else {
        let title = e.currentTarget.dataset.title;
        wx.navigateTo({ url: `/pages/SignUp/SignUp?title=${title}&id=${that.data.id}` });
      }
    }).catch((err) => {
      console.error('请求出错:', err);
    });
  },
  onLoadPDF(){
    const that =this
    // console.log(that.data.info.meetingDocumentsUrl)
    wx.downloadFile({
      url: that.data.info.meetingDocumentsUrl,
      success: function (res) {
        const filePath = res.tempFilePath  // 临时文件路径
        wx.openDocument({
          filePath: filePath,
          showMenu: true // 预览文件右上方的...
        })
      }
    })

    /*    const that =this
        util.request(api.onLoadePDF,{fileUrl:that.data.info.meetingDocumentsUrl},'get').then(function(res){
          console.log(res,"@@@@@")
          const downloadTask = wx.downloadFile({
            url: that.data.info.meetingDocumentsUrl,
            success: function (res) {
              if (res.statusCode === 200) {
                // 文件下载成功，保存到本地临时目录
                const tempFilePath = res.tempFilePath;
                // 可选：保存文件到相册或本地文件系统
                wx.saveFile({
                  tempFilePath,
                  success: function (saveRes) {
                    const savedFilePath = saveRes.savedFilePath;
                    console.log("文件保存成功:", savedFilePath);
                    // 提示用户文件已保存或执行其他操作
                  },
                  fail: function (err) {
                    console.error("保存文件失败:", err);
                  }
                });
              } else {
                console.warn("下载文件失败，状态码:", res.statusCode);
              }
            },
            fail: function (err) {
              console.error("下载文件失败:", err);
            }
          });
        })*/
  }
});
